<?php

namespace AdminPanel\LessonBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AdminPanel\DashboardBundle\Utils\adminCustomClass;
use Symfony\Component\HttpFoundation\Response;

class LessonController extends Controller
{
  public function listAction()
  {

    if (isset($_POST['beginDate'])) {
    $beginDate = adminCustomClass::clear($_POST['beginDate']);
  } else {
    $beginDate = date('Y-m-d', strtotime("-1 month"));
  }

  if (isset($_POST['endDate'])) {
    $endDate = adminCustomClass::clear($_POST['endDate']);
  } else {
    $endDate = date('Y-m-d', strtotime("now"));
  }

  if (isset($_POST['type'])) {
    $type = (int)$_POST['type'];
  } else {
    $type = null;
  }

  if (isset($_POST['tutor'])) {
    $tutor = adminCustomClass::clear($_POST['tutor']);
  } else {
    $tutor = null;
  }

  if (isset($_POST['student'])) {
    $student = adminCustomClass::clear($_POST['student']);
  } else {
    $student = null;
  }

  if (isset($_POST['page'])) {
    $page = adminCustomClass::clear($_POST['page']);
  } else {
    $page = 1;
  }

  $perPage = $this->container->getParameter('perPage');

  $lessonList = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:Lesson')->findLessons($page, $perPage, $beginDate, $endDate, $type, $tutor, $student);

  $totalLessonCount = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:Lesson')->findLessonsCount($beginDate, $endDate, $type, $tutor, $student);

  $tutorList = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:Tutor')->findAllTutorForSelect();
  $studentList = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:User')->findAllUserForSelect();

  return $this->render('AdminPanelLessonBundle:Lesson:list.html.twig', array(            
            "lessonList" => $lessonList,
            "totalLessonCount" => $totalLessonCount,
            "tutorList" => $tutorList,
            "studentList" => $studentList,
            "page" => $page,
            "beginDate" => $beginDate,
            "endDate" => $endDate,
            "type" => $type,
            "tutor" => $tutor,
            "student" => $student,
          ));
  }
    
  public function lessonStatusNameChangeAction($status)
  {
    switch ($status) {
      case 'Үүссэн' : {
          $result = '<button style="font-size: 9px;width: 70px;" class="btn btn-block btn-warning">Үүссэн</button>';
          break;
        }
      case 'Сурагч зөвшөөрсөн' : {
          $result = '<button style="font-size: 9px;width: 70px;" class="btn btn-block btn-success">Сурагч зөвшөөрсөн</button>';
          break;
        }        
      case 'Гэрээ үүссэн' : {
          $result = '<button style="font-size: 9px;width: 70px;" class="btn btn-block btn-success">Гэрээ үүссэн</button>';
          break;
        }
      case 'Эхэлсэн' : {
          $result = '<button style="font-size: 9px;width: 70px;" class="btn btn-block btn-success">Эхэлсэн</button>';
          break;
        }
      case 'Сурагч татгалзсан' : {
          $result = '<button style="font-size: 9px;width: 90px;" class="btn btn-block btn-danger">Сурагч татгалзсан</button>';
          break;
        }
      case 'Админ татгалзсан' : {
          $result = '<button style="font-size: 9px;width: 90px;" class="btn btn-block btn-danger">Админ татгалзсан</button>';
          break;
        }
      case 'Устгасан' : {
          $result = '<button style="font-size: 9px;width: 90px;" class="btn btn-block btn-danger">Устгасан</button>';
          break;
        }
      case '0' : {        
          $result = '<button style="font-size: 9px;width: 90px;" class="btn btn-block btn-danger">Идэвхигүй</button>';
          break;
      }
      case '1' : {
          $result = '<button style="font-size: 9px;width: 70px;" class="btn btn-block btn-success">Идэвхитэй</button>';
          break;
        }
      default : {
          $result = '';
          break;
        }
    }

    return new Response($result);
  }
  
  public function showAction($id)
  {
    $id = (int) $id;
    $lessonMassive = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:Lesson')->findLessonById($id);    
    
    $invoiceArray = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:Lesson')->findInvoiceByLessonId($id);
    
    $bankAttemptArray = $this->getDoctrine()->getManager()->getRepository('AdminPanelDashboardBundle:Lesson')->findBankAttemptByLessonId($id);
    
//    var_dump($bankAttemptArray);die;
    return $this->render('AdminPanelLessonBundle:Lesson:show.html.twig', array(
                "lesson" => $lessonMassive,
                "id" => $id,
                "invoiceArray" => $invoiceArray
            ));
  }
}
